﻿using Core;
using Core.Data;
using Core.Domain.Seo;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Services.Seo
{
    /// <summary>
    /// 友情链接服务实现类
    /// </summary>
    public partial class FriendlyLinkService :IFriendlyLinkService
    {        
        #region Fields

        private readonly IRepository<FriendlyLink> _friendlylinkRepository;

        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        public FriendlyLinkService(IRepository<FriendlyLink> friendlylinkRepository)
        {
            this._friendlylinkRepository = friendlylinkRepository;
        }

        #endregion

        #region Method
        /// <summary>
        /// 删除友情链接
        /// </summary>
        /// <param name="entity"></param>
        public virtual void DeleteFriendlyLink(FriendlyLink entity)
        {
            if (entity == null)
                throw new ArgumentNullException("FriendlyLink");

            _friendlylinkRepository.Delete(entity);
        }

        /// <summary>
        /// 新增友情链接
        /// </summary>
        /// <param name="entity"></param>
        public virtual void InsertFriendlyLink(FriendlyLink entity)
        {
            if (entity == null)
                throw new ArgumentNullException("FriendlyLink");

            _friendlylinkRepository.Insert(entity);
        }

        /// <summary>
        /// 更新友情链接
        /// </summary>
        /// <param name="entity"></param>
        public virtual void UpdateFriendlyLink(FriendlyLink entity)
        {
            if (entity == null)
                throw new ArgumentNullException("FriendlyLink");

            _friendlylinkRepository.Update(entity);
        }

        /// <summary>
        /// 根据主键获取友情链接
        /// </summary>
        /// <param name="friendlyLinkId"></param>
        /// <returns></returns>
        public virtual FriendlyLink GetFriendlyLinkById(int friendlyLinkId)
        {
            if (friendlyLinkId == 0)
                return null;

            return _friendlylinkRepository.GetById(friendlyLinkId);
        }

        /// <summary>
        /// 获取所有友情链接
        /// </summary>
        /// <param name="active"></param>
        /// <param name="top"></param>
        /// <returns></returns>
        public virtual IList<FriendlyLink> GetFriendlyLinks(bool? active = true, bool? top = false)
        {
            var query = _friendlylinkRepository.Table;
            if (active.HasValue)
                query = query.Where(s => s.Active == active);

            if (top.HasValue)
                query = query.Where(s => s.Top == top);

            return query.ToList();
        }

        /// <summary>
        /// 分页获取所有友情链接
        /// </summary>
        /// <param name="name"></param>
        /// <param name="active"></param>
        /// <param name="top"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public virtual IPagedList<FriendlyLink> GetFriendlyLinks(string name = "", bool? active = true, bool? top = true, int pageIndex = 0, int pageSize = int.MaxValue)
        {
            var query = _friendlylinkRepository.Table;

            if (!String.IsNullOrWhiteSpace(name))
                query = query.Where(s => s.Name.Contains(name));

            if (active.HasValue)
                query = query.Where(s => s.Active == active);

            if (top.HasValue)
                query = query.Where(s => s.Top == top);

            query = query.OrderBy(s=>s.DisplayOrder);

            return new PagedList<FriendlyLink>(query, pageIndex, pageSize);
        }
        #endregion
    }
}
